// 引入 订单服务api
import { searchServices, putServices ,addServices,delServices } from "../../api/orderServices";



const state = {
  tableDataServices: [], // 数据数组
  pageServices: {
    currentPage: 0, // 当前显示页
    total: 0, // 总条数
    size: 0, // 每页显示条数
    totalPage: 0, // 总页数
  }

};



// 修改仓库状态
const mutations = {

  tableDataServicesMutations(state, payload) {
    state.tableDataServices = payload;
  },
  pageServicesMutations(state, payload) {
    state.pageServices = payload;
  },
};

// 管理异步请求
const actions = {

  tableDataServicesActions(context, payload) {

    searchServices(payload).then((res) => {
      console.log("sds", res);
      let page = {
        currentPage: parseInt(res.current), // 当前显示页
        total: parseInt(res.total), // 总条数
        size: parseInt(res.size), // 每页显示条数
        totalPage: parseInt(res.totalPage), // 总页数
      };

      // 时间戳转为日期函数
      function timestampToTime(timestamp) {
        let date = new Date(timestamp * 1000);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '年';
        let M = (date.getMonth() + 1) + '月';
        let D = date.getDate() + '日 ';
        let h = date.getHours() + ':';
        let m = date.getMinutes();
        return Y + M + D + h + m;
      }

      res.rows.map((item, index) => {
        // console.log(item.time);
        return (
          item.time = timestampToTime(item.time), // 下单时间
          item.orderTime = timestampToTime(item.orderTime) // 预约时间
        )

      });

      context.commit("tableDataServicesMutations", res.rows);
      context.commit("pageServicesMutations", page);
    });

  },


    // 修改
  putServicesActions(context, payload) {
    console.log("payload", payload);   // 修改里 ，传进来的payload 为数组 如：[{id:“此为id”},{“此对象为你要修改的内容”},]
    putServices(payload[0].id, payload[1]).then((res) => {
      console.log("sds", res);
    })
  },


  // 增加
  addServicesActions(context, payload) {
    console.log("payload", payload);
    addServices(payload).then((res) => {
      console.log("addsds", res);
    })
  },

  // 删除
  delServicesActions(context, payload) {
    console.log("payload", payload);
    delServices(payload).then((res) => {
      console.log("delsds", res);
    })
  }




};

export default {
  // namespaced: true,
  state,
  // getters,
  mutations,
  actions
};
